
package com.loong.loong.hik.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.api.entity.HikFaceGroup;
import com.loong.loong.hik.api.entity.resource.HikFaceGroupResult;
import com.loong.loong.hik.entity.FaceGroup;
import com.loong.loong.hik.mapper.FaceGroupMapper;
import com.loong.loong.hik.property.result.face.FaceGroupResult;
import com.loong.loong.hik.property.result.face.FaceGroupResultVo;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.FaceGroupService;
import com.loong.loong.hik.service.HikFaceVService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 人脸分组
 *
 * @author jinyiang
 * @date 2023-03-28 15:40:49
 */
@Service
public class FaceGroupServiceImpl extends ServiceImpl<FaceGroupMapper, FaceGroup> implements FaceGroupService {

	@Resource
	private HikFaceVService hikFaceVService;

	/**
	 * 新增人脸分组
	 * @param faceGroup
	 * @return
	 */
	@Override
	public R add(FaceGroup faceGroup) {
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			HikFaceGroup hikFaceGroup = new HikFaceGroup();
			hikFaceGroup.setName(faceGroup.getName());
			hikFaceGroup.setDescription(faceGroup.getDescription());
			try {
           //API 调用的结果存储在FaceGroupResult对象中
				FaceGroupResult faceGroupResult = hikFaceVService.addGroup(hikFaceGroup);
				if (faceGroupResult==null){
					return R.failed("接口返回值为空！");
				}
				if ("0".equals(faceGroupResult.getCode())){
					FaceGroupResult.Data data = faceGroupResult.getData();
					if (data!=null){
						faceGroup.setIndexCode(data.getIndexCode());
					}
				}else{
					return R.failed("接口调用失败，原因："+faceGroupResult.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		FaceGroup faceGroup1 = new FaceGroup();

		faceGroup1.setIndexCode(faceGroup.getIndexCode());

		long count = count(Wrappers.query(faceGroup1));

		if (count>0){
			return R.failed("已存在相同的编号,编号："+faceGroup.getIndexCode());
		}else{
			return R.ok(save(faceGroup));
		}
	}

	/**
	 * 删除人脸分组
	 * @param id
	 * @return
	 */
	@Override
	public R del(Integer id) {

		FaceGroup faceGroup = getById(id);
		if (faceGroup==null){
			R.failed("删除失败，根据id查询为空");
		}
		if (StringUtils.isEmpty(faceGroup.getIndexCode())){
			R.failed("删除失败，indexCode为空");
		}
		if (HikFaceUrl.SWITCH.equals("Y")){
			HikFaceGroupResult hikFaceGroup = new HikFaceGroupResult();
			List<String> strings = new ArrayList<>();
			strings.add(faceGroup.getIndexCode());
			hikFaceGroup.setIndexCodes(strings);
			try {
				FaceGroupResultVo faceGroupResultVo = hikFaceVService.delGroup(hikFaceGroup);
				if (faceGroupResultVo==null){
					return R.failed("接口返回值为空！");
				}
				if (!"0".equals(faceGroupResultVo.getCode())){
					return R.failed("接口调用失败，原因："+faceGroupResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}



		return R.ok(removeById(id));
	}

	/**
	 * 修改人脸分组
	 * @param faceGroup
	 * @return
	 */
	@Override
	public R update(FaceGroup faceGroup) {

		if (HikFaceUrl.SWITCH.equals("Y")){
			HikFaceGroup hikFaceGroup = new HikFaceGroup();
			hikFaceGroup.setIndexCode(faceGroup.getIndexCode());
			hikFaceGroup.setName(faceGroup.getName());
			hikFaceGroup.setDescription(faceGroup.getDescription());
			try {
				FaceGroupResultVo faceGroupResultVo = hikFaceVService.updateGroup(hikFaceGroup);
				if (faceGroupResultVo==null){
					return R.failed("接口返回值为空！");
				}
				if (!"0".equals(faceGroupResultVo.getCode())){
					return R.failed("接口调用失败，原因："+faceGroupResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return R.ok(updateById(faceGroup));

	}
}
